<template>
  <div class="app-container">
    <eForm ref="form"/>
    <!--工具栏-->
    <div class="head-container">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">

        <el-form-item>
          <el-input label="id" v-model="dataForm.id" clearable placeholder="id" style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="driverClassName" v-model="dataForm.driverClassName" clearable placeholder="driverClassName"
                    style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="jdbcUrlBase" v-model="dataForm.jdbcUrlBase" clearable placeholder="jdbcUrlBase"
                    style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="database" v-model="dataForm.database" clearable placeholder="database" style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="jdbcUrlConfig" v-model="dataForm.jdbcUrlConfig" clearable placeholder="jdbcUrlConfig"
                    style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="username" v-model="dataForm.username" clearable placeholder="username" style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="password" v-model="dataForm.password" clearable placeholder="password" style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="fileType" v-model="dataForm.fileType" clearable placeholder="fileType" style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="fileOutputDir" v-model="dataForm.fileOutputDir" clearable placeholder="fileOutputDir"
                    style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="ignoreTableNameList" v-model="dataForm.ignoreTableNameList" clearable
                    placeholder="ignoreTableNameList" style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="ignorePrefix" v-model="dataForm.ignorePrefix" clearable placeholder="ignorePrefix"
                    style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="ignoreSuffix" v-model="dataForm.ignoreSuffix" clearable placeholder="ignoreSuffix"
                    style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="version" v-model="dataForm.version" clearable placeholder="version" style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input label="description" v-model="dataForm.description" clearable placeholder="description"
                    style="width: 200px;"
                    class="filter-item"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button @click="getDataList()" class="filter-item" size="mini" type="success" icon="el-icon-search">查询
          </el-button>
          <el-button v-if="this.checkPermission(['admin','efficiencytools:generatorDatabaseDocumentConfig:save'])"
                     type="primary"
                     class="filter-item" size="mini"
                     @click="addOrUpdateHandle()">新增
          </el-button>
          <el-button v-if="this.checkPermission(['admin','efficiencytools:generatorDatabaseDocumentConfig:delete'])"
                     type="danger"
                     class="filter-item" size="mini"
                     @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除
          </el-button>
          <el-button v-if="this.checkPermission(['admin','efficiencytools:generatorDatabaseDocumentConfig:download'])"
                     type="primary"
                     class="filter-item" size="mini"
                     @click="downloadFile()">下载
          </el-button>
          <el-button v-if="this.checkPermission(['admin','efficiencytools:generatorDatabaseDocumentConfig:importData'])"
                     type="primary"
                     class="filter-item" size="mini"
                     @click="importDataFromFile()">从excel导入
          </el-button>
        </el-form-item>
      </el-form>
    </div>


    <el-table :data="data" v-loading="loading" ref="table" size="small" @selection-change="selectionChangeHandle"
              style="width: 100%;">

      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="id">
      </el-table-column>
      <el-table-column prop="driverClassName" label="driverClassName">
      </el-table-column>
      <el-table-column prop="jdbcUrlBase" label="jdbcUrlBase">
      </el-table-column>
      <el-table-column prop="database" label="database">
      </el-table-column>
      <el-table-column prop="jdbcUrlConfig" label="jdbcUrlConfig">
      </el-table-column>
      <el-table-column prop="username" label="username">
      </el-table-column>
      <el-table-column prop="password" label="password">
      </el-table-column>
      <el-table-column prop="fileType" label="fileType">
      </el-table-column>
      <el-table-column prop="fileOutputDir" label="fileOutputDir">
      </el-table-column>
      <el-table-column prop="ignoreTableNameList" label="ignoreTableNameList">
      </el-table-column>
      <el-table-column prop="ignorePrefix" label="ignorePrefix">
      </el-table-column>
      <el-table-column prop="ignoreSuffix" label="ignoreSuffix">
      </el-table-column>
      <el-table-column prop="version" label="version">
      </el-table-column>
      <el-table-column prop="description" label="description">
      </el-table-column>
      <el-table-column
        v-if="this.checkPermission(['admin','efficiencytools:generatorDatabaseDocumentConfig:delete','efficiencytools:generatorDatabaseDocumentConfig:update'])"
        fixed="right" width="150px" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" v-permission="['admin','efficiencytools:generatorDatabaseDocumentConfig:update']"
                     size="mini" @click="addOrUpdateHandle(scope.row.id)">
            修改
          </el-button>
          <el-button type="primary" v-permission="['admin','efficiencytools:generatorDatabaseDocumentConfig:delete']"
                     size="mini" @click="deleteHandle(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页组件-->
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>

    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
    import checkPermission from '@/utils/permission'
    import initData from '@/mixins/initData'
    import {parseTime} from '@/utils/index'
    import {downloadFileFunction} from '@/api/downloadFile'
    import AddOrUpdate from './generatorDatabaseDocumentConfigAddOrUpdate'
    import {
        queryGeneratorDatabaseDocumentConfigListByConditionMap2,
        downloadDataToExcel,
        deleteGeneratorDatabaseDocumentConfigByIntegerIdList
    } from '@/api/GeneratorDatabaseDocumentConfig.js'
    import eForm from './GeneratorDatabaseDocumentConfigImportDataFromFile'

    export default {
        name: 'GeneratorDatabaseDocumentConfig',
        components: {AddOrUpdate, eForm},
        mixins: [initData],
        data() {
            return {
                dataForm: {
                    id: '',
                    driverClassName: '',
                    jdbcUrlBase: '',
                    database: '',
                    jdbcUrlConfig: '',
                    username: '',
                    password: '',
                    fileType: '',
                    fileOutputDir: '',
                    ignoreTableNameList: '',
                    ignorePrefix: '',
                    ignoreSuffix: '',
                    version: '',
                    description: ''
                },
                dataListLoading: false,
                dataListSelections: [],
                addOrUpdateVisible: false,
                exportDataLoading: false,
            }
        },
        created() {
            this.$nextTick(() => {
                this.init()
            })
        },
        methods: {
            parseTime,
            checkPermission,
            beforeInit() {
                this.url = 'api/efficiencyTools/generatorDatabaseDocumentConfig/queryGeneratorDatabaseDocumentConfigListByConditionMap2'
                const sort = 'sort,asc'
                this.params = {page: this.page, pageSize: this.size, sort: sort};
                this.getParam()
                return true
            },
            importDataFromFile() {
                this.$refs.form.dialog = true
            },
            downloadFile() {
                let outputFileName = `GeneratorDatabaseDocumentConfigList查询导出结果.xlsx`;
                this.getParam()
                downloadDataToExcel(this.params).then(res => {
                    this.exportDataLoading = false
                    downloadFileFunction(res, outputFileName)
                    this.init()
                    this.$notify({
                        title: '下载成功',
                        type: 'success',
                        duration: 2500
                    })
                }).catch(err => {
                    this.exportDataLoading = false
                    console.log(err.response.data.message)
                })
            },
            selectionChangeHandle(val) {
                this.dataListSelections = val
            },
            getParam() {
                const sort = 'sort,asc'
                this.params = {page: this.page, pageSize: this.size, sort: sort};
                const query = this.dataForm;
                const id = query.id;
                if (id) {
                    this.params['id'] = id
                }
                const driverClassName = query.driverClassName;
                if (driverClassName) {
                    this.params['driverClassName'] = driverClassName
                }
                const jdbcUrlBase = query.jdbcUrlBase;
                if (jdbcUrlBase) {
                    this.params['jdbcUrlBase'] = jdbcUrlBase
                }
                const database = query.database;
                if (database) {
                    this.params['database'] = database
                }
                const jdbcUrlConfig = query.jdbcUrlConfig;
                if (jdbcUrlConfig) {
                    this.params['jdbcUrlConfig'] = jdbcUrlConfig
                }
                const username = query.username;
                if (username) {
                    this.params['username'] = username
                }
                const password = query.password;
                if (password) {
                    this.params['password'] = password
                }
                const fileType = query.fileType;
                if (fileType) {
                    this.params['fileType'] = fileType
                }
                const fileOutputDir = query.fileOutputDir;
                if (fileOutputDir) {
                    this.params['fileOutputDir'] = fileOutputDir
                }
                const ignoreTableNameList = query.ignoreTableNameList;
                if (ignoreTableNameList) {
                    this.params['ignoreTableNameList'] = ignoreTableNameList
                }
                const ignorePrefix = query.ignorePrefix;
                if (ignorePrefix) {
                    this.params['ignorePrefix'] = ignorePrefix
                }
                const ignoreSuffix = query.ignoreSuffix;
                if (ignoreSuffix) {
                    this.params['ignoreSuffix'] = ignoreSuffix
                }
                const version = query.version;
                if (version) {
                    this.params['version'] = version
                }
                const description = query.description;
                if (description) {
                    this.params['description'] = description
                }
            },
            getDataList() {
                this.getParam()
                this.page = 0
                this.params['page'] = this.page
                queryGeneratorDatabaseDocumentConfigListByConditionMap2(this.params).then(res => {
                    this.data = res.content;
                    this.total = res.totalElements;
                }).catch(() => {
                    this.loading = false;
                })
            },
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle(id) {
                let ids = id ? [id] : this.dataListSelections.map(item => {
                    return item.id
                })
                console.log(ids)
                const idList = [];
                for (let i = 0; i < ids.length; i++) {
                    idList.push(ids[i])
                }
                console.log(idList)
                let params = {
                    'idList': idList.toString()
                }
                console.log(params)
                this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteGeneratorDatabaseDocumentConfigByIntegerIdList(params).then(res => {
                        if (res.hasError) {
                            this.$notify({
                                title: res.error.message,
                                type: 'fail',
                                duration: 2500
                            })
                        } else {

                            this.$notify({
                                title: res.data,
                                type: 'success',
                                duration: 2500
                            })

                        }
                        this.getDataList()
                    }).catch(err => {
                        this.loading = false
                        console.log(err.response.data.message)
                    })
                })
            }
        }
    }
</script>

